package com.xinbochuang.machine.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xinbochuang.machine.admin.domain.Macinout;
import com.xinbochuang.machine.admin.domain.MacinoutAssociation;
import com.xinbochuang.machine.admin.mapper.MacinoutAssociationMapper;
import com.xinbochuang.machine.admin.service.IMacCheckRecordService;
import com.xinbochuang.machine.admin.service.IMacinoutAssociationService;
import com.xinbochuang.machine.admin.service.IMacinoutService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 机房进出定时任务
 *
 * @author 黄晓鹏
 * @date 2021-09-11 19:20
 */
@Component
@Slf4j
public class MacInOutTask {

    @Resource
    private IMacinoutService macinoutService;
    @Resource
    private IMacinoutAssociationService iMacinoutAssociationService;

    @Resource
    MacinoutAssociationMapper macinoutAssociationMapper;
    @Resource
    IMacCheckRecordService iMacCheckRecordService;

    /**
     * 定时将进出表中结束日期小于当前时间的工单设为待验收
     */
    @Scheduled(cron = "0 0 12 * * ?")
    public void execute() {
        log.info("进出工单定时任务启动：====================================");
        // 定时任务查找的是条件是：未删除/结束时间<当前时间/工单状态为已审核
        List<Macinout> list = macinoutService.list(Wrappers.<Macinout>lambdaQuery()
                .eq(Macinout::getDeleteFlag, 0)
                .eq(Macinout::getOrderStatus, 0));
                //.lt(Macinout::getWorkEndTime, new Date()));
        log.info("查找到匹配的工单共：" + list.size() + " 个");
        List<Macinout> newList = new ArrayList<>();
        List<MacinoutAssociation> assList = new ArrayList<>();
        for (Macinout macinout : list) {
            Macinout entity = new Macinout();
            entity.setUuid(macinout.getUuid());
            entity.setOrderStatus(4);
            newList.add(entity);

            QueryWrapper<MacinoutAssociation> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(MacinoutAssociation::getDeleteFlag, 0);
            queryWrapper.lambda().eq(MacinoutAssociation::getMacinoutId, macinout.getUuid());

            //记录工单审核状态
            iMacCheckRecordService.insertCheckRecord(macinout.getLoginUserId()+"","4",macinout.getUuid());

            List<MacinoutAssociation> macList = macinoutAssociationMapper.selectList(queryWrapper);
            for (MacinoutAssociation macinoutassociation:macList) {
                MacinoutAssociation macinoutAssociation=new MacinoutAssociation();
                macinoutAssociation.setOrderStatus(4);
                macinoutAssociation.setMacinoutId(macinout.getUuid());
                macinoutAssociation.setUuid(macinoutassociation.getUuid());
                assList.add(macinoutAssociation);
            }
        }
        // 修改状态
        if (!newList.isEmpty()) {
            macinoutService.updateBatchById(newList);
            iMacinoutAssociationService.updateBatchById(assList);
        }
    }

}
